package com.example.dmgame.activity;

import java.util.ArrayList;
import java.util.List;

import com.example.dmgame.R;
import com.example.dmgame.adapter.ViewPagerAdapter;
import com.example.dmgame.bean.ChapterListItem;
import com.example.dmgame.fragment.ForumFragment;
import com.example.dmgame.fragment.GameFragment;
import com.example.dmgame.fragment.HeaderNullFragment;
import com.example.dmgame.fragment.NewsFragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnClickListener,OnPageChangeListener{
	RadioButton articleBtn,forumBtn,gameBtn;
	RadioGroup group;
	ViewPager viewPager;
	TextView topTitle;
	
	FragmentManager manager;
	
	NewsFragment newsFragment;
	ForumFragment forumFragment;
	GameFragment gameFragment;
	HeaderNullFragment headerNullFragment;
	
	private ViewPagerAdapter adapter;
	List<Fragment> fragments;
	
	List<ChapterListItem> list = new ArrayList<ChapterListItem>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		manager = getSupportFragmentManager();
		
		topTitle = (TextView)this.findViewById(R.id.top_title);
		viewPager = (ViewPager)this.findViewById(R.id.article_center);
		
		group = (RadioGroup)findViewById(R.id.bottom_radioGroup);
		
		articleBtn = (RadioButton) findViewById(R.id.artcleBtn);
		forumBtn = (RadioButton)findViewById(R.id.forumBtn);
		gameBtn = (RadioButton) findViewById(R.id.gameBtn);
		
		manager = getSupportFragmentManager();
		adapter = new ViewPagerAdapter(manager);
		
		newsFragment = new NewsFragment();
		forumFragment = new ForumFragment();
		gameFragment = new GameFragment();
		
		headerNullFragment = new HeaderNullFragment();

		
		fragments = new ArrayList<Fragment>();
		fragments.add(newsFragment);
		fragments.add(forumFragment);
		fragments.add(gameFragment);
		adapter.initData(MainActivity.this, fragments);
		viewPager.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		viewPager.setOnPageChangeListener(this);
		
		articleBtn.setOnClickListener(this);
		forumBtn.setOnClickListener(this);
		gameBtn.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.artcleBtn:
			topTitle.setText("文章");
			viewPager.setCurrentItem(0);
			break;
		case R.id.forumBtn:
			topTitle.setText("论坛");
			viewPager.setCurrentItem(1);
			break;
		case R.id.gameBtn:
			topTitle.setText("游戏");
			viewPager.setCurrentItem(2);
		default:
			break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		// Log.i("MainActivity", "----arg0--->>"+arg0);
		Log.i("MainActivity", "----arg1--->>"+arg1);
		Log.i("MainActivity", "----arg2--->>"+arg2);
	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		Log.i("MainActivity", "----arg0--->>"+arg0);
//		FragmentTransaction transaction = manager.beginTransaction();
		if(arg0==0){
			articleBtn.setChecked(true);
			topTitle.setText("文章");
//			transaction.replace(R.id.header, headerFragment);
//			transaction.commit();
		}else if(arg0==1){
			forumBtn.setChecked(true);
			topTitle.setText("论坛");
//			transaction.replace(R.id.header, headerNullFragment);
//			transaction.commit();
		}else{
			gameBtn.setChecked(true);
			topTitle.setText("游戏");
			//transaction.replace(R.id.header, headerNullFragment);
			//transaction.commit();
		}
	}

}
